﻿package com.shangda.util;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.mina.common.IoBuffer;
import org.apache.mina.common.IoSession;

import com.shangda.entity.ReceiveData;
import com.shangda.entity.Sewage;
import com.shangda.service.DataService;

public class RequestHandel {
	
	//处理老系统 8990端口
	public static List<ReceiveData> dataHandelOrigin(byte[] shows,int count,IoSession session,List<Integer> timeChanges,Map<String,Byte> switchStatus) throws Exception{
		
		List<ReceiveData> receiveDatas = new ArrayList<ReceiveData>();
		
		//网页传送数据，该站点时间已经更新
		if (count == 4){
			if (shows[0] == '#'){
				int sewageId = shows[1]*127 + shows[2];
					if (!timeChanges.contains(sewageId)){
						timeChanges.add(sewageId);
						switchStatus.put(sewageId+"",shows[3]);
						}
					session.write(IoBuffer.wrap(new byte[]{35,35}));
					}
			return receiveDatas;
			
		}
		
		
		
		byte[] atom = new byte [70];
		//拆分一个分包
		for ( int begin=0; (begin<count && ((begin+1) < count));){

				if (shows[begin] == '#' && shows[begin+1] == '#'){
					int j = 0;
					
					//取出符合格式的70个字节的数据包长度
					int i = begin;
					for (;((i<(begin+70)) && (i<count));i++){
						atom [j] = shows[i];
						j++;
					}
					
					if (i-begin<70){
						break;
					}
					
					
					//将byte转化成short类型
					short [] datas = byteToShort(atom);
					
					//解析这70个字节的数据
					ReceiveData temp = DataConvert.getReceiveData70(datas);
					if (temp != null){
							receiveDatas.add(temp);
					}
					begin = begin + 70;
				}else{
					begin ++;
				}
			}
		
		//发送给下位机
		if (receiveDatas.size() > 0){
			
			if (timeChanges.contains(receiveDatas.get(0).getSewageId())){
				
				byte [] status = new byte[3];
				
				status[0] = 35;
				status[1] = 35;
				status[2] = switchStatus.get(receiveDatas.get(0).getSewageId()+"");
						
				//发送byte数组
				session.write(IoBuffer.wrap(status));
						
				//重timeChanges中删除时间
				int index = timeChanges.indexOf(receiveDatas.get(0).getSewageId());
				timeChanges.remove(index);
				switchStatus.remove(receiveDatas.get(0).getSewageId()+"");
			}
			
			
			
		}
		
		
		
		
		//时间设置
		if (receiveDatas.size() > 0){
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			Date date = new Date();
			date.setTime(date.getTime()-receiveDatas.size()*60000);
			for (ReceiveData receiveData : receiveDatas){
				date.setTime(date.getTime()+60000);
				receiveData.setTime(formatter.format(date));
			}
		}
		
		
		return	receiveDatas;
		
	}

		
		
	
	
	
	
	
	
	//江都水务处理逻辑，端口号为8991
	public static List<ReceiveData> dataHandelJD(byte [] shows,int count,IoSession session,DataService dataService,List<Integer> timeChanges) throws Exception{
		
		List<ReceiveData> receiveDatas = new ArrayList<ReceiveData>();
		
		
		//网页传送数据，该站点时间已经更新
		if (count == 3){
			if (shows[0] == '#'){
				int sewageId = shows[1]*127 + shows[2];
				if (!timeChanges.contains(sewageId)){
					timeChanges.add(sewageId);
					}
				session.write(IoBuffer.wrap(new byte[]{35,35}));
				}
			return receiveDatas;
		}
		
		
		//先获取下位机的所有分包
		byte[] atom = new byte [76];
		for ( int begin=0; (begin<count && ((begin+1) <count));){
			if (shows[begin] == '#' && shows[begin+1] == '#'){
				int j = 0;
				
				//取出符合格式的76个字节的数据包长度
				int i = begin;
				for (;(i<begin+76 && i<count);i++){
					atom [j] = shows[i];
					j++;
				}
				if (i-begin<76){
					break;
				}
				//将byte转化成short类型
				short [] datas = byteToShort(atom);
				
				//解析这76个字节的数据
				ReceiveData temp = DataConvert.getReceiveData76(datas);
				if (temp != null){
					receiveDatas.add(temp);
				}
				begin = begin + 76;
				continue;
			}
			
			
			//存储时间数据
			if (shows[begin] == '#' && shows[begin+1] == 'A'){
				
				int j = 0;
				
				//取出符合格式的32个字节的数据包长度
				int i = begin;
				for (;i<begin+32 & i<count;i++){
					atom [j] = shows[i];
					j++;
				}
				
				if (i-begin < 32){
					break;
				}
				
				//传送各设备的时间
				if (atom[0] == '#'&& atom[1] == 'A'){
					int[] times = new int[14];
					times[0] = atom[2]*127 + atom[3]; times[1] = atom[4]*127 + atom[5];
					times[2] = atom[6]*127 + atom[7]; times[3] = atom[8]*127 + atom[9];
					times[4] = atom[10]*127 + atom[11]; times[5] = atom[12]*127 + atom[13];
					times[6] = atom[14]*127 + atom[15]; times[7] = atom[16]*127 + atom[17];
					times[8] = atom[18]*127 + atom[19]; times[9] = atom[20]*127 + atom[21];
					times[10] = atom[22]*127 + atom[23]; times[11] = atom[24]*127 + atom[25];
					times[12] = atom[26]*127 + atom[27]; times[13] = atom[28]*127 + atom[29];
					int sewageId = atom[30]*127 + atom[31];
					//跟新对应de时间
					dataService.updateSewage(sewageId, times);
				}
				begin = begin+32;
				continue;
				
			}
			
			
			//发送连接请求
			if (shows[begin]=='#' && shows[begin+1]=='B'){
				 session.write(IoBuffer.wrap(new byte[] {35,1}));
				 
				 begin = begin+2;
				 continue;
			}
			
			
			begin++;
			
		}
		
		//发送跟新时间给下位机
		if (receiveDatas.size() > 0){
			if (timeChanges.contains(receiveDatas.get(0).getSewageId())){
				//查找时间根据sewageId
				Sewage sewage = dataService.findSewageById(receiveDatas.get(0).getSewageId());
						
				//发送跟新时间数据
				byte [] times = new byte[37];
						
				times[0] = 35;times[1] = 35;
				times[2] = (byte) (sewage.getRuntimeperiod1()/127);times[3] = (byte) (sewage.getRuntimeperiod1()%127);
				times[4] = (byte) (sewage.getStoptimeperiod1()/127);times[5] = (byte) (sewage.getStoptimeperiod1()%127);
				times[6] = (byte) (sewage.getRuntimeperiod2()/127);times[7] = (byte) (sewage.getRuntimeperiod2()%127);
				times[8] = (byte) (sewage.getStoptimeperiod2()/127);times[9] = (byte) (sewage.getStoptimeperiod2()%127);
				times[10] = (byte) (sewage.getRuntimeperiod3()/127);times[11] = (byte) (sewage.getRuntimeperiod3()%127);
				times[12] = (byte) (sewage.getStoptimeperiod3()/127);times[13] = (byte) (sewage.getStoptimeperiod3()%127);
				times[14] = (byte) (sewage.getRuntimeperiod4()/127);times[15] = (byte) (sewage.getRuntimeperiod4()%127);
				times[16] = (byte) (sewage.getStoptimeperiod4()/127);times[17] = (byte) (sewage.getStoptimeperiod4()%127);
				times[18] = (byte) (sewage.getRuntimeperiod5()/127);times[19] = (byte) (sewage.getRuntimeperiod5()%127);
				times[20] = (byte) (sewage.getStoptimeperiod5()/127);times[21] = (byte) (sewage.getStoptimeperiod5()%127);
				times[22] = (byte) (sewage.getRuntimeperiod6()/127);times[23] = (byte) (sewage.getRuntimeperiod6()%127);
				times[24] = (byte) (sewage.getStoptimeperiod6()/127);times[25] = (byte) (sewage.getStoptimeperiod6()%127);
				times[26] = (byte) (sewage.getRuntimeperiod7()/127);times[27] = (byte) (sewage.getRuntimeperiod7()%127);
				times[28] = (byte) (sewage.getStoptimeperiod7()/127);times[29] = (byte) (sewage.getStoptimeperiod7()%127);
						
				//泵状态的扩展30-36
						
				//发送byte数组
				session.write(IoBuffer.wrap(times));
						
				//重timeChanges中删除时间
				int index = timeChanges.indexOf(receiveDatas.get(0).getSewageId());
				timeChanges.remove(index);		
			}
		}
		
		
		//时间设置
		if (receiveDatas.size() > 0) {
			SimpleDateFormat formatter = new SimpleDateFormat(
					"yyyy/MM/dd HH:mm:ss");
			Date date = new Date();
			date.setTime(date.getTime() - receiveDatas.size() * 60000);
			for (ReceiveData receiveData : receiveDatas) {
				date.setTime(date.getTime() + 60000);
				receiveData.setTime(formatter.format(date));
			}
		}
		
		
		return receiveDatas;
	}
	
	
	
	//面源项目处理  端口号： 9990
	public static List<ReceiveData> dataHandelAS(byte[] shows, int count)
			throws Exception {

		List<ReceiveData> receiveDatas = new ArrayList<ReceiveData>();

		byte[] atom = new byte[106];
		// 拆分一个分包
		for (int begin = 0; (begin < count && ((begin + 1) < count));) {

			if (shows[begin] == '#' && shows[begin + 1] == '#') {
				int j = 0;

				// 取出符合格式的106个字节的数据包长度
				int i = begin;
				for (; ((i < (begin + 106)) && (i < count)); i++) {
					atom[j] = shows[i];
					j++;
				}

				if (i - begin < 106) {
					break;
				}

				// 将byte转化成short类型
				short[] datas = byteToShort(atom);

				// 解析这98个字节的数据
				ReceiveData temp = DataConvert.getReceiveData106(datas);
				if (temp != null) {

					receiveDatas.add(temp);
				}
				begin = begin + 106;
			} else {
				begin++;
			}
		}

		// 时间设置
		if (receiveDatas.size() > 0) {
			SimpleDateFormat formatter = new SimpleDateFormat(
					"yyyy/MM/dd HH:mm:ss");
			Date date = new Date();
			date.setTime(date.getTime() - receiveDatas.size() * 60000);
			for (ReceiveData receiveData : receiveDatas) {
				date.setTime(date.getTime() + 60000);
				receiveData.setTime(formatter.format(date));
			}
		}
		return receiveDatas;

	}

	
	
	public static short [] byteToShort(byte[] shows){
		short [] datas = new short[shows.length];
		for (int i=0;i<shows.length;i++){
			datas[i] = (short) (shows[i] & 0xFF);
		}
		return datas;
	}
	
	
	
	
	/*public static short [] byteToShort(byte[] shows){
		short [] datas = new short[shows.length];
		String hex = "";
		for (int i=0;i<shows.length;i++){
			hex = Integer.toHexString(shows[i]&0xFF);
			if (hex.length() == 1){
				hex = "0" + hex;
			}
			datas[i] = Short.parseShort(hex, 16);
		}
		return datas;
	}*/
	
	
	
	

}
